import { useRouter } from "@/router";
import { hasPagePermission } from "@/utils/permission";
import { ElMessage } from "element-plus";
const router = useRouter();


// 导出列表数据
export function exportListData(tableName="导出列表", options, data = []) {
  let tHeader = [], tData = [];
  data.forEach((item, index) => {
    tData[index] = [];
    options.forEach((prop) => {
      if (index === 0) {
        tHeader.push(prop.name);
      }
      tData[index].push(item[prop.key]);
    });
  });
  import("@/vendor/Export2Excel").then((excel) => {
    excel.export_json_to_excel({
      header: tHeader,
      data: tData,
      filename: tableName + new Date().toLocaleDateString(),
      autoWidth: true,
      bookType: "xlsx",
    });
  });
}

// 保留小数小数
export function dealNumer(number,save=2){
  let num = parseFloat(number);
  if(isNaN(num)){
    throw new Error("param is NaN.");
  }
  return num.toFixed(save)*1;
}

export function dealAmount(amount,dividend=100){
  if((amount??true) === true)return;
  let amount2 = parseFloat(amount);
  if(isNaN(amount2))throw new Error("param is not a number.");
  amount2 = amount2/dividend;
  return "¥ "+ (amount2.toFixed(2)||"0.00");
}

export function isWebLink(link){
  if(!link){return false}
  const reg=/^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.).+$/;
  return reg.test(link);
}

//  内容（作品）
export const contentList = {
  status: [
    {key: 0,name:"下架"},
    {key: 1,name:"上架"},
  ],
  // pico审核状态
  adminStatus: [
    {key: 0,name:"待审核"},
    {key: 1,name:"已通过"},
    {key: 2,name:"已拒绝"},
  ],
  // 阿里审核状态
  auditStatus: [
    {key: 0,name:"待审核"},
    {key: 1,name:"已通过"},
    {key: 2,name:"已拒绝"},
    {key: 3,name:"人工已通过"},
  ],
}
//  举报管理相关
export const reportList = {
  //  举报原因
  reason:[
    {key: "色情低俗",name:"色情低俗"},
    {key: "政治敏感",name:"政治敏感"},
    {key: "违法有害",name:"违法有害"},
    {key: "广告骚扰",name:"广告骚扰"},
    {key: "人身攻击",name:"人身攻击"},
    {key: "诈骗",name:"诈骗"},
    {key: "其他",name:"其他"},
  ],
  // 举报目标
  target:[
    {key: 0,name:"用户主页"},
    {key: 1,name:"私信聊天"},
    {key: 2,name:"作品"},
  ],
  // 举报结果
  result:[
    {key: 0,name:"封号"},
    {key: 1,name:"下架作品"},
    {key: 2,name:"未违规"},
  ],
  // 举报状态
  status:[
    {key: 0,name:"未处理"},
    {key: 1,name:"已处理"},
  ],
}
// 引导页
export const guideList = {
  used: [
    {key: 0,name:"未使用"},
    {key: 1,name:"使用中"},
  ],
}
// 意见反馈
export const feedbackList = {
  status: [
    {key: 0,name:"未处理"},
    {key: 1,name:"已处理"},
  ],
  type: [
    {key: 1, name: "设置"},
    {key: 2, name: "百科搜索列表"},
    {key: 3, name: "百科内容"},
  ]
}
// 标签管理
export const tagList = {
  status: [
    {key: 0,name:"停用"},
    {key: 1,name:"启用"},
  ],
}
// 消息
export const messageList = {
  status: [
    {key: 0,name:"已停止"},
    {key: 1,name:"已推送"},
    {key: 2,name:"待推送"},
  ],
}
// 用户
export const userList = {
  sex: [
    {key: 0,name:"中性"},
    {key: 1,name:"男"},
    {key: 2,name:"女"},
  ],
  identification:[
    {key: 0,name:"普通"},
    {key: 1,name:"平台"},
    {key: 2,name:"达人"},
  ],
  status:[
    {key: 0,name:"封禁"},
    {key: 1,name:"正常"},
    {key: 2,name:"注销"},
  ],
  vipType:[
    {key: 0,name:"普通（关闭认证）"},
    {key: 1,name:"类型A"},
    {key: 2,name:"类型B"},
    {key: 3,name:"类型C"},
    {key: 4,name:"类型D"},
  ],
}
// 账户
export const accountList = {
  // 小饼干（积分）交易类型
  biscuitType:[
    // { key: 1, name: "寄售商品" },
    // { key: 2, name: "厂家货补" },
    // { key: 3, name: "积分提现" },
    // { key: 4, name: "退款" },
    // { key: 5, name: "推广商品" },
    // { key: 6, name: "收益" },
    // { key: 7, name: "积分消费" },
    // { key: 8, name: "活动" },
    // { key: 9, name: "回款" },
    { key: 1, name: "打赏" },
    { key: 2, name: "提现" },
    { key: 3, name: "活动奖励" },
    { key: 4, name: "取消活动奖励" },
  ],
  // 福利金交易类型
  welfareType: [
    { key: 1, name: "兑换" },
    { key: 2, name: "消费" },
    { key: 3, name: "退款" },
    { key: 4, name: "活动奖励" },
  ],
  endpoint:[
    { key: "QMY", name: "趣摸鱼" },
    { key: "GXD", name: "供销端" },
    { key: "PICO", name: " 品客" },
  ],
  // 提现状态（pico）
  withdrawProcess:[
    { key: 1, name: "待审核"},
    { key: 2, name: "银行处理"},
    { key: 3, name: "平台审核：未通过"},
    { key: 4, name: "到账成功"},
    // { key: 5, name: "银行处理：未通过"},
  ],
  // 提现状态（qumoyu）
  withdrawStatus:[
    { key: 1, name: "待审核"},
    { key: 2, name: "待转账"},
    { key: 3, name: "已拒绝"},
    { key: 4, name: "已完成"},
  ],
  accountType:[
    { key: 0, name: "支付宝"},
    { key: 1, name: "微信"},
    { key: 2, name: "银行卡"},
  ]
}
// 任务
export const taskList = {
  isJump: [
    {key: 0,name:"不跳转"},
    {key: 1,name:"跳转"},
  ],
  jumpType: [
    {key: "H5",name:"H5"},
    {key: "APP",name:"APP"},
  ],
  appTarget:[
    {key: 1,name:"首页"},
    {key: 2,name:"创作-选择素材"},
    {key: 3,name:"个人主页-编辑资料"},
  ],
  methods:[
    { key: 1,name: "每日首次登录",type: 1 },
    { key: 2,name: "分享作品",type: 1 },
    { key: 3,name: "注册成功",type: 1 },
    { key: 4,name: "完善个人信息",type: 1 },
    { key: 11,name: "发布作品",type: 3 },
    { key: 12,name: "作品被精选",type: 3 },
    { key: 21,name: "被转发",type: 2 },
    { key: 22,name: "被点赞",type: 2 },
    { key: 23,name: "被收藏",type: 2 },
    { key: 24,name: "被评论",type: 2 },
    { key: 25,name: "视频播放次数（普通）",type: 2 },
    { key: 26,name: "视频播放次数（完播）",type: 2 },
    { key: 31,name: "用户提交绿皮书",type: 1 },
    { key: 32,name: "绿皮书被核实",type: 1 },
    { key: 33,name: "绿皮书邀请填写成功",type: 1 }
  ],
  rewardCategory:[
    {key: 1,name:"小饼干"},
    {key: 2,name:"实物"},
    {key: 3,name:"优惠券"},
  ],
  rewardType:[
    { key: 0,name: "不固定" },
    { key: 1,name: "固定"},
  ],
  limitType:[
    { key: 0,name: "不限制" },
    { key: 1,name: "限制"},
  ],
  rewardShow:[
    {key: 1,name:"当日已完成/总奖励"},
    {key: 2,name:"总累计奖励"},
    {key: 3,name:"今日累积奖励"},
  ],
  // 奖励衰减类型
  attenuationType:[
    { key: 0,name: "不衰减" },
    { key: 1,name: "按照发布日期衰减"},
  ],
  tagColor: [
    { key: "#FFA500",name: "黄色" },
    { key: "#FF5400",name: "橙色"},
    { key: "#FA384A",name: "红色" },
  ]
}

// 活动
export const activityList = {
  status: [
    { key: 0, name: "未开始" },
    { key: 1, name: "进行中" },
    { key: 2, name: "已过期" },
    { key: 3, name: "已取消" },
  ],
  isSignUp:[
    { key: 0,name: "否" },
    { key: 1,name: "是"},
  ],
  settlement:[
    { key: -1,name: "奖励截止时间" },
    { key: 0,name: "立即结算"},
    { key: 7,name: "7天后"},
    { key: 15,name: "15天后"},
    { key: 30,name: "30天后"},
  ],
  rewardStatus:[
    { key: 0, name: "待结算"},
    { key: 1, name: "可用"},
    { key: 2, name: "已撤销"},
  ],
  auditStatus:[
    { key: 0, name: "未审核"},
    { key: 1, name: "审核通过"},
    { key: 2, name: "审核未通过"},
  ],
  openMethods:[
    { key: 0, name: "手动开启+自动开启"},
    { key: 1, name: "自动开启"},
    { key: 2, name: "手动开启"},
  ],
  timesLimit:[
    { key: 0, name: "当天列表红包领取结束，次日刷新"},
    { key: 1, name: "列表红包领完后不再刷新"},
  ],
  inviteStatus:[
    { key: 0, name: "邀请有礼"},
    { key: 1, name: "活动分享"},
  ]

}

// 活动
export const eventsList = {
  status: [
    { key: 0, name: "下架" },
    { key: 1, name: "上架" }
  ],
  sex:[
    { key: '0',name: "男" },
    { key: '1',name: "女"},
  ],
  protocolType:[
    { key: '0',name: "活动订单协议"},
    { key: '1',name: "洗护订单协议"},
  ],
  reviewStatus:[
    { key: '1', name: "待审核"},
    { key: '2', name: "审核未通过"},
    { key: '3', name: "审核通过"},
  ],
  auditStatus:[
    { key: 0, name: "未审核"},
    { key: 1, name: "审核通过"},
    { key: 2, name: "审核未通过"},
  ],
  openMethods:[
    { key: 0, name: "手动开启+自动开启"},
    { key: 1, name: "自动开启"},
    { key: 2, name: "手动开启"},
  ],
  timesLimit:[
    { key: 0, name: "当天列表红包领取结束，次日刷新"},
    { key: 1, name: "列表红包领完后不再刷新"},
  ],
  inviteStatus:[
    { key: 0, name: "注册成功"},
    { key: 1, name: "邀请成功"},
  ]

}
// 配置
export const configList = {
  bannerLocation: [
    {key: "mine", name: "我的"},
    {key: "activity_center", name: "活动中心"},
    {key: "events_index", name: "活动管理"},
    {key: "cracker_index", name: "小饼干首页"},
    {key: "discover_index", name: "首页发现"},
    {key: "toiletry_center", name: "洗护首页"},
    {key: "nearby_banner", name: "附近轮播"},
  ],
}
// 科普知识库
export const knowledgeList = {
  petRace: [
    { key: "dog", name: "犬"},
    { key: "cat", name: "猫"},
  ],
  type:[
    { key: "live", name: "生活"},
    { key: "food", name: "食物"},
  ],
  status:[
    { key: 0,name: "禁用"},
    { key: 1,name: "启用"},
  ],
  contentType:[
    { key: 0,name: "成长周期"},
    { key: 1,name: "综合表述"},
  ],
  doType:[
    { key: 1,name: "能做"},
    { key: 2,name: "不能做"},
    { key: 3,name: "慎做"},
  ],
  eatType:[
    { key: 1,name: "禁吃"},
    { key: 2,name: "慎吃"},
    { key: 3,name: "可吃"},
  ]
}
// 血库
export const bloodList = {
  status:[
    { key: 0,name: "已下架" },
    { key: 1,name: "活跃中" },
    { key: 2,name: "待点亮" },
    { key: 3,name: "已失效" },
    { key: 4,name: "审核失败" },
    { key: 5,name: "审核中" },
  ],
  desireStatus: [
    { key: 0,name: "关闭" },
    { key: 1,name: "已开启" },
  ]
}


export function getItemFromArray(list,value,config){
  /**
   * @param list：源数组
   * @param value：查找的值
   * @param config{
   *  key: 'key',//查找的值的键
   *  name: 'name',//获取的值的键
   *  item: false,//获取整条数据
   * }
   */
  const {key='key',name='name',item=false} = config||{};
  for(let prop of list||[]){
    if(prop[key] == value){
      return item?prop:prop[name];
    }
  }
  return item?{}:undefined;
}


export function isLongTime(time){
  // 判断时间是否大于2300年
  let t = new Date(time).getTime();
  let t2300 = new Date("2300-01-01 00:00:00").getTime();
  return t>=t2300;
}

export function gotoUserDetail(id,view){
  let hasPermission = hasPagePermission("VideouserList");
  if(!hasPermission){
    ElMessage.warning("暂无权限！");
    return;
  };
  router.push(`/videouser/detail/${id}${view?'?view='+view:''}`);
}

export function fillImageUrl(url = '') {
  if(!url){return "";}
  const base = 'https://img.qumoyugo.com'
  let isFullUrl = /^http|https/.test(url);
  if(isFullUrl){
    return url;
  } else{
    return base + url;
  }
}

export function getMonthNumber({st,et},options={}) {
  // st:开始时间
  // et:结束时间
  /*
    options: {
      round: 'down'（向下取整）|'up'（向上取整）|'rounding'（四舍五入取整）|undefined（不取整）
      dealYear: boolean, // 是否处理年，如 res:15 => res={year:1,month:3}
    }
  */
 /**
  * return {
  *   month: number,
  *   year: number,
  * }
  */
  if(!st) return;
  let s_date = new Date(st);
  let e_date = et?new Date(et):new Date();
  let month = getMonthNum(s_date,e_date);
  let year = 0;
  switch(options.round){
    case 'down':
      month = Math.floor(month);
      break;
    case 'up':
      month = Math.ceil(month);
      break;
    case 'rounding':
      month = Math.round(month);
      break;
  }
  if (options?.dealYear) {
    year = ~~(month/12);
    month = month%12;
  }
  return {year,month}

  function getMonthNum (s_date, e_date) {
    // s_date, e_date: new Date();
    // return 1.002155
    let a_31month = [1, 3, 5, 7, 8, 10, 12];//31天的月份，2月份也暂时按30天计算
    let pl_month = e_date.getMonth() == 0 ? 12 : e_date.getMonth();
    let t_prelastmonthdays = a_31month.indexOf(pl_month) >= 0 ? 31 : 30;//结束月上个月的天数
    let t_lastmonthdays = a_31month.indexOf(e_date.getMonth()+1) >= 0 ? 31 : 30;//结束月的天数
    let predictnum = 0;

    let t_year = e_date.getFullYear() - s_date.getFullYear();
    let t_month = e_date.getMonth() - s_date.getMonth();
    let t_day = (e_date.getDate()+1) - s_date.getDate();//结束日期比开始日期少一天，算一整月
    if (t_day < 0) {
      t_month = t_month - 1;
      predictnum = (t_prelastmonthdays + t_day) / t_lastmonthdays;//多出的不足一个月的天数除以结束月的天数
    } else {
      predictnum = t_day / t_lastmonthdays;
    }
    return (t_year * 12) + t_month + predictnum;
  }

}

// 优惠卷
export const couponList = {
  //使用类型
  couponUseType:[
    { key: 0,name: "立减券" },
    { key: 1,name: "满减券" },
  ],
  //类型
  couponType: [
    { key: 0,name: "活动优惠券" },
    { key: 1,name: "洗护优惠券" },
  ],
  //用户类型
  userType: [
    { key: 0,name: "不限" },
    { key: 1,name: "新用户" },
    { key: 2,name: "老用户" },
    { key: 3,name: "指定用户" },
  ],
  //状态
  couponStatus: [
    { key: 0,name: "下架" },
    { key: 1,name: "上架" },
  ],
  //是否显示在领券中心
  showPage: [
    { key: 0,name: "否" },
    { key: 1,name: "是" },
  ]

}
